// 通用增删改查接口管理

import request from "@/utils/request";

// 分页查询
export const commonPageQuery = async <T>(url: string, data: any) =>
    request.post<any, { code: number; message: string, data: { records: T[]; total: number } }>(url, data);

// 删除
export const commonRemove = async (url: string, id: number) =>
    request.delete<any, { code: number; message: string, data: boolean }>(`${url}/${id}`);

// 批量删除
export const commonBatchRemove = (url: string, idList: number[]) =>
    request.delete<any, { code: number; message: string, data: boolean }>(url, { data: idList });

// 根据ID获取单条数据
export const commonGetById = async <T>(url: string, id: number) =>
    request.get<any, { code: number; message: string, data: T }>(`${url}/${id}`);

// 列表查询
export const commonListQuery = async <T>(url: string) =>
    request.get<any, { code: number; message: string, data: T[] }>(url);

// 保存(新增)
export const commonSave = async <T>(url: string, data: T) =>
    request.post<any, { code: number; message: string, data: T }>(url, data);

// 编辑
export const commonEdit = async <T>(url: string, data: T) =>
    request.put<any, { code: number; message: string, data: T }>(url, data)

// 编辑或删除
export const commonCreateOrEdit = async <T>(url: string, data: T) =>
    request.post<any, { code: number; message: string, data: T }>(url, data);

// 改变某个id的状态
export const commonChangeStatus = (url: string, id: number, status: number) =>
    request.post<any, { code: number; message: string, data: boolean }>(`${url}/${id}/${status}`);